package com.weds.edpf.core.web;

import com.weds.core.annotation.Logs;
import com.weds.core.base.BaseCommPager;
import com.weds.core.base.BaseController;
import com.weds.core.base.BasePageSearch;
import com.weds.core.resp.JsonResult;
import com.weds.edpf.core.entity.DtPhotoEntity;
import com.weds.edpf.core.entity.UserEntity;
import com.weds.edpf.core.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @Author zcz
 * @Description User管理
 * @Date 2020-06-02
 */
@RestController("userControllerNew")
@RequestMapping(value = "/userNew")
@Api(value = "User管理", description = "User管理")
public class UserController extends BaseController{

    private Logger log = LogManager.getLogger();

    @Resource(name = "userServiceNew")
    private UserService userService;
    @Logs
    @ApiOperation(value = "查询人员序号", notes = "查询人员序号")
    @RequestMapping(value = "/moduleUser", method = RequestMethod.GET)
    public JsonResult<String> selectModuleUser() throws Exception {
        String moduleUser = userService.selectModuleUser();
        return succMsgData(moduleUser);
    }
    @Logs
    @ApiOperation(value = "加载新增人员界面", notes = "加载新增人员界面")
    @RequestMapping(value = "/displayFields", method = RequestMethod.GET)
    public JsonResult<Object> displayFields() {
        Map<String, List<Map<String, String>>> displayField = userService.displayFields();
        return succMsgData(displayField);
    }

    @Logs
    @ApiOperation(value = "新增User信息", notes = "新增User信息")
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public JsonResult<Object> insert(@RequestBody @Valid UserEntity record) {
        return userService.insertSelective(record);
    }

    @Logs
    @ApiOperation(value = "更新User信息", notes = "更新User信息")
    @RequestMapping(value = "/update", method = RequestMethod.PUT)
    public JsonResult<Object> update(@RequestBody @Valid UserEntity record) {
        return userService.updateByPrimaryKeySelective(record);
    }

    @Logs
    @ApiOperation(value = "人员调动", notes = "人员调动")
    @RequestMapping(value = "/transfer", method = RequestMethod.PUT)
    public JsonResult<Object> transfer(@RequestParam("depSerial") String depSerial,@RequestParam("userSerial")String userSerial) {
        JsonResult<Object> result = userService.transfer(depSerial, userSerial);
        return result;
    }

    @Logs
    @ApiOperation(value = "人员离职", notes = "人员离职")
    @RequestMapping(value = "/quit", method = RequestMethod.PUT)
    public JsonResult<Object> quit(@RequestParam("userSerial")String userSerial) {
        JsonResult<Object> result = userService.quit(userSerial);
        return result;
    }

    @Logs
    @ApiOperation(value = "离职人员调动", notes = "离职人员调动")
    @RequestMapping(value = "/quitTransfer", method = RequestMethod.PUT)
    public JsonResult<Object> quitTransfer(@RequestParam("depSerial") String depSerial,@RequestParam("userSerial")String userSerial) {
        JsonResult<Object> result = userService.quitTransfer(depSerial, userSerial);
        return result;
    }

    @Logs
    @ApiOperation(value = "人员复职", notes = "人员复职")
    @RequestMapping(value = "/reinstatement", method = RequestMethod.PUT)
    public JsonResult<Object> reinstatement(@RequestParam("userSerial")String userSerial) {
        JsonResult<Object> result = userService.reinstatement(userSerial);
        return result;
    }

    @Logs
    @ApiOperation(value = "查询User清单", notes = "查询User清单")
    @RequestMapping(value = "/selectListPageByEntity", method = RequestMethod.POST)
    public JsonResult<BaseCommPager<UserEntity>> selectListPageByEntity(@RequestBody BasePageSearch<UserEntity> record) {
        setPageHelper(record);
        UserEntity entity = record.getSearch();
        if (entity == null) {
            entity = new UserEntity();
        }
        List<UserEntity> list = userService.selectListByEntity(entity);
        return succMsgData(new BaseCommPager<>(list));
    }

    @Logs
    @ApiOperation(value = "查询User列表", notes = "查询User列表")
    @RequestMapping(value = "/selectListByEntity", method = RequestMethod.POST)
    public JsonResult<List<UserEntity>> selectListByEntity(@RequestBody UserEntity record) {
        List<UserEntity> list = userService.selectListByEntity(record);
        return succMsgData(list);
    }


    @Logs
    @ApiOperation(value = "查询User信息", notes = "查询User信息")
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.GET)
    public JsonResult<UserEntity> selectByPrimaryKey(@RequestParam Long userSerial) {
        UserEntity entity = userService.selectByPrimaryKey(userSerial);
        return succMsgData(entity);
    }

    @Logs
    @ApiOperation(value = "查看基本信息", notes = "查看基本信息")
    @RequestMapping(value = "/getUserBasicInfo", method = RequestMethod.GET)
    public JsonResult<Object> getUserBasicInfo(@RequestParam String userSerial) {
        Map<String, String> info = userService.getUserBasicInfoByUserSerial(userSerial);
        return succMsgData(info);
    }
    @Logs
    @ApiOperation(value = "查看照片", notes = "查看照片")
    @RequestMapping(value = "/getPhoto", method = RequestMethod.GET)
    public JsonResult<Object> getPhoto(@RequestParam String userSerial) {
        DtPhotoEntity photo = userService.getPhoto(userSerial);
        return succMsgData(photo);
    }

}